package com.xls.box2023.jifen.duihuanjibi;

import android.text.TextUtils;
import android.view.View;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.lzz.bottomencapsulation.adapter.MvpBaseRecyclerAdapter;
import com.lzz.bottomencapsulation.consts.EventbusCode;
import com.lzz.bottomencapsulation.eventbus.MessageEvent;
import com.lzz.bottomencapsulation.manager.RiskManager;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.lzz.bottomencapsulation.net.RequestCallBackTwo;
import com.lzz.bottomencapsulation.utils.AppUtils;
import com.lzz.bottomencapsulation.utils.TUtils;
import com.xls.box2023.Consts.IntentConsts;
import com.xls.box2023.Consts.NetCode;
import com.xls.box2023.adapter.DuiHuanJiBiTabAdapter;
import com.xls.box2023.adapter.DuiHuanJinBiBiLiAdapter;
import com.xls.box2023.customcontrol.ErrDialog;
import com.xls.box2023.customcontrol.OkDialog;
import com.xls.box2023.entity.AppsRulesEntity;
import com.xls.box2023.entity.DuiHuanJiBiInfoEntity;
import com.xls.box2023.entity.JiFenMingXiEntity;
import com.xls.box2023.entity.JiFenTiXianBiliEntity;
import com.xls.box2023.entity.UserEntity;
import com.xls.box2023.jifen.JiFenNetWorkUtil;
import com.xls.box2023.jinbi.JinBiNetWorkUtil;
import com.xls.box2023.login.LoginNetworkUtil;
import com.xls.box2023.login.LoginUtil;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.box2023.network.CallbackT;
import com.xls.box2023.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;

/**
 * MVPPlugin
 */

public class DuiHuanJiBiPresenter extends BasePresenterImpl<DuiHuanJiBiContract.View> implements DuiHuanJiBiContract.Presenter{


    DuiHuanJiBiTabAdapter adapter;
    ArrayList<AppsRulesEntity> datalist=new ArrayList<>();
    @Override
    public RecyclerView.Adapter getAdapter() {
        if (adapter==null){
            adapter=new DuiHuanJiBiTabAdapter(context,getType(), datalist, new MvpBaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    for (AppsRulesEntity appsRulesEntity:datalist){
                        appsRulesEntity.setSelector(false);
                    }
                    if (datalist.get(position).getExchangeCount()>0){
                        datalist.get(position).setSelector(true);
                    }
                    adapter.notifyDataSetChanged();
                }
            });
        }
        return adapter;
    }

    @Override
    public void duihuan() {
        String score="";
        String score_text="";
        for (AppsRulesEntity appsRulesEntity:datalist){
            if (appsRulesEntity.isSelector()){
                score=appsRulesEntity.getId();
                score_text=appsRulesEntity.getExchangeValue();
            }
        }
        if (TextUtils.isEmpty(score)){
            TUtils.showShort(context,"请选择提现额度");
            return;
        }
        getPermissions(score,score_text);

    }

    private void getScoreExchange(String score){
        JiFenNetWorkUtil.getInstance().getScoreExchange(score, getType(),getAPPId(),getPackageName(), new RequestCallBackTwo<DuiHuanJiBiInfoEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(DuiHuanJiBiInfoEntity requestData, String msg) {
                if (activity==null||mView==null||requestData==null)return;
//                TUtils.showShort(context,"兑换成功");
                getSourceData();
                String content="";
                if (TextUtils.equals(requestData.getSubsidyScoreStr(),"0")){
                    content="兑换成功";
                }else {
                    content="兑换成功\n奖励 "+requestData.getSubsidyScoreStr()+"元宝";
                }
//                JiFenDuiHuanJiLuActivity.startActivity(context,getAPPId(),getType());
                OkDialog okDialog=new OkDialog(context,content);
                okDialog.show();
            }

            @Override
            public void onRequestfail(String errno, Object errmsg) {
                if (TextUtils.equals(errno, NetCode.code_200)){
                    getSourceData();
                    DuiHuanJiBiInfoEntity requestData= (DuiHuanJiBiInfoEntity) errmsg;
                    if (requestData==null)return;
                    String content="";
                    if (TextUtils.equals(requestData.getSubsidyScoreStr(),"0")){
                        content="兑换成功，审核中～";
                    }else {
                        content="兑换成功，审核中～\n 审核通过后，奖励 "+requestData.getSubsidyScoreStr()+"元宝";
                    }
//                    JiFenDuiHuanJiLuActivity.startActivity(context,getAPPId(),getType());
                    OkDialog okDialog=new OkDialog(context,content);
                    okDialog.show();
                }else {
                    ErrDialog errDialog=new ErrDialog(context, (String) errmsg);
                    errDialog.show();
                }
            }
        });
    }
    @Override
    public void getPermissions(String score,String score_text) {
        XXPermissions.with(activity)
                // 申请单个权限
                .permission(Permission.READ_PHONE_STATE)
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_PHONE_NUMBERS)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.READ_MEDIA_VIDEO)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            LoginNetworkUtil.getInstance().getLoginParams2(activity, new CallbackT<Map<String, String>>() {
                                @Override
                                public void callback(Map<String, String> stringStringMap) {
                                    stringStringMap.put("appName", AppUtils.getPackageName(activity));
                                    stringStringMap.put("token", SPUtils.getLoginToken());
                                    fkCheck(stringStringMap,score,score_text);
                                }
                            });
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            XXPermissions.startPermissionActivity(activity, permissions);
                        } else {
                            TUtils.showShort(context,"请授予权限,并重新登陆");
                            LoginUtil.getInstance().outLogin(activity);
                        }
                    }
                });
    }

    @Override
    public void fkCheck(Map<String, String> stringStringMap, String score,String score_text) {
        RiskManager.getInstance().reportRisk(RiskManager.withdrawal);
        stringStringMap.put("withdrawal_amount",score_text);
        LoginNetworkUtil.getInstance().fkCheck(activity,"box_jifenduihuan",stringStringMap, new RequestCallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(String requestData) {
                getScoreExchange(score);
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }


    public String getType() {
        return activity.getIntent().getStringExtra(IntentConsts.type);
    }

    public String getAPPId(){
        return activity.getIntent().getStringExtra(IntentConsts.id);
    }

    public String getPackageName(){
        return activity.getIntent().getStringExtra(IntentConsts.packageName);
    }

    @Override
    public void getSourceData() {
        super.getSourceData();
        getAccountByAppId();
        JinBiNetWorkUtil.getInstance().getAppRules(getAPPId(),getPackageName(), getType(),new RequestCallBack<ArrayList<AppsRulesEntity>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(ArrayList<AppsRulesEntity> requestData) {
                if (activity==null||context==null||getView()==null)return;
                datalist.clear();
                datalist.addAll(requestData);
                adapter.notifyDataSetChanged();
                if (datalist.size()==0){
                    mView.showTiXianBtnState(false);
                }else {
                    mView.showTiXianBtnState(true);
                }
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

    public void getAccountByAppId(){
        EventBus.getDefault().post(new MessageEvent(EventbusCode.CODE_TIXIAN_OK));//更新积分
        JinBiNetWorkUtil.getInstance().getAccountByAppId(getAPPId(),getPackageName(), new RequestCallBack<JiFenMingXiEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(JiFenMingXiEntity requestData) {
                if (mView==null||requestData==null||context==null)return;
                mView.showData(requestData);
                getUserLevel(requestData);
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

    public void  getScoreExchangeRateRules(String level){
        JinBiNetWorkUtil.getInstance().getScoreExchangeRateRules(getAPPId(),getPackageName(), new RequestCallBack<ArrayList<JiFenTiXianBiliEntity>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(ArrayList<JiFenTiXianBiliEntity> requestData) {
                if (activity==null||getView()==null||context==null||requestData==null)return;
                for (JiFenTiXianBiliEntity jiFenTiXianBiliEntity:requestData){
                    if (TextUtils.equals(jiFenTiXianBiliEntity.getLevel(),level))jiFenTiXianBiliEntity.setSelector(true);
                }
                jiFenTiXianBiliEntities.clear();
                jiFenTiXianBiliEntities.addAll(requestData);
                duiHuanJinBiBiLiAdapter.notifyDataSetChanged();

            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });

    }

    private void getUserLevel(JiFenMingXiEntity jiFenMingXiEntity){
        JinBiNetWorkUtil.getInstance().getUserLevel(getAPPId(),getPackageName(), new RequestCallBack<UserEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(UserEntity requestData) {
                if (activity==null||getView()==null||context==null||requestData==null)return;
                mView.showLevel(requestData,jiFenMingXiEntity);
                getScoreExchangeRateRules(requestData.getLevel());
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

    DuiHuanJinBiBiLiAdapter duiHuanJinBiBiLiAdapter;
    ArrayList<JiFenTiXianBiliEntity> jiFenTiXianBiliEntities=new ArrayList<>();
    public RecyclerView.Adapter getBiliAdapter() {
        if (duiHuanJinBiBiLiAdapter==null){
            duiHuanJinBiBiLiAdapter=new DuiHuanJinBiBiLiAdapter(context,jiFenTiXianBiliEntities);
        }
        return duiHuanJinBiBiLiAdapter;
    }
}
